class Solution(object):
    def advantageCount(self, nums1, nums2):
        nums1 = [[num, index] for index, num in enumerate(nums1)]
        nums2 = [[num, index] for index, num in enumerate(nums2)]
        nums1.sort()
        nums2.sort()
        n = len(nums1)
        out = [-1] * n
        p = [0] * n
        index1 = 0
        index2 = 0
        while index1 < n:
            if nums1[index1][0] > nums2[index2][0]:
                out[nums2[index2][1]] = nums1[index1][0]
                p[index1] = 1
                index1 += 1
                index2 += 1
            else:
                index1 += 1
        index1 = 0
        index2 = 0
        while index2 < n:
            if out[index2] >= 0:
                index2 += 1
            else:
                if p[index1]:
                    index1 += 1
                else:
                    out[index2] = nums1[index1][0]
                    index1 += 1
                    index2 += 1
        return out


data = Solution()
nums1 = [2, 7, 11, 15]
nums2 = [1, 10, 4, 11]
print(data.advantageCount(nums1, nums2))
nums1 = [12, 24, 8, 32]
nums2 = [13, 25, 32, 11]
print(data.advantageCount(nums1, nums2))
